﻿using DapperBaseDal.Connections;
using DapperExtensions;
using DapperExtensions.Mapper;
using DapperExtensions.Sql;
using System.Collections.Generic;
using System.Data;
using System.Reflection;

namespace DapperBaseDal
{
    public class BuildFactory
    {
        /// <summary>
        /// 生成数据库连接对象
        /// </summary>
        /// <returns></returns>
        public static IDbConnection CreateConnection(string strConn, DatabaseType databaseType = DatabaseType.MySql)
        {
            IDbConnection conn = null;
            //获取配置进行转换
            switch (databaseType)
            {
                case DatabaseType.SqlServer:
                    conn = new SqlServerProvider().GetConnection(strConn);
                    break;
                case DatabaseType.MySql:
                    conn = new MySqlProvider().GetConnection(strConn);
                    break;
                case DatabaseType.Sqlite:
                    conn = new SqliteProvider().GetConnection(strConn);
                    break;
                case DatabaseType.Oracle:
                    conn = new OracleProvider().GetConnection(strConn);
                    break;
            }
            return conn;
        }

        /// <summary>
        /// 生成 DapperImplementor
        /// </summary>
        /// <param name="databaseType"></param>
        /// <returns></returns>
        public static DapperAsyncImplementor CreateDapperImplementor(DatabaseType databaseType = DatabaseType.MySql)
        {
            DapperAsyncImplementor _dapper = null;
            //获取配置进行转换
            switch (databaseType)
            {
                case DatabaseType.SqlServer:
                    var sqlconfig = new DapperExtensionsConfiguration(typeof(AutoClassMapper<>), new List<Assembly>(), new SqlServerDialect());
                    var sqlGenerator = new SqlGeneratorImpl(sqlconfig);
                    _dapper = new DapperAsyncImplementor(sqlGenerator);
                    break;
                case DatabaseType.MySql:
                    var mysqlconfig = new DapperExtensionsConfiguration(typeof(AutoClassMapper<>), new List<Assembly>(), new MySqlDialect());
                    var mysqlGenerator = new SqlGeneratorImpl(mysqlconfig);
                    _dapper = new DapperAsyncImplementor(mysqlGenerator);
                    break;
                case DatabaseType.Sqlite:
                    var sqliteconfig = new DapperExtensionsConfiguration(typeof(AutoClassMapper<>), new List<Assembly>(), new SqliteDialect());
                    var sqliteGenerator = new SqlGeneratorImpl(sqliteconfig);
                    _dapper = new DapperAsyncImplementor(sqliteGenerator);
                    break;
                case DatabaseType.Oracle:
                    var oracleconfig = new DapperExtensionsConfiguration(typeof(AutoClassMapper<>), new List<Assembly>(), new OracleDialect());
                    var oracleGenerator = new SqlGeneratorImpl(oracleconfig);
                    _dapper = new DapperAsyncImplementor(oracleGenerator);
                    break;
                case DatabaseType.DB2:
                    var db2config = new DapperExtensionsConfiguration(typeof(AutoClassMapper<>), new List<Assembly>(), new DB2Dialect());
                    var db2Generator = new SqlGeneratorImpl(db2config);
                    _dapper = new DapperAsyncImplementor(db2Generator);
                    break;
                case DatabaseType.PostgreSql:
                    var postgreSqlconfig = new DapperExtensionsConfiguration(typeof(AutoClassMapper<>), new List<Assembly>(), new PostgreSqlDialect());
                    var postgreSqlGenerator = new SqlGeneratorImpl(postgreSqlconfig);
                    _dapper = new DapperAsyncImplementor(postgreSqlGenerator);
                    break;
            }
            return _dapper;
        }
    }
}
